<?php
$message = "";
$config = dirname(__FILE__).'/wp-config.php';
require_once($config);  
if( isset($_POST['ourl']) && isset($_POST['nurl']) ){ 
  $ourl = $_POST['ourl'];
  $nurl = $_POST['nurl'];
  if(!file_exists($config)){
    $message = '<div class="error">Error: Config file is not found.</div>';
  } elseif (!strlen(trim($ourl)) || !strlen(trim($nurl))){
    $message = '<div class="error">Error: Original URL and New URL could not be blank.</div>';
  } else {
    $ourl = str_replace('http://', '', str_replace('https://', '', $ourl));
    $nurl = str_replace('http://', '', str_replace('https://', '', $nurl));
    if(substr($ourl, -1) == '/'){
      $ourl = substr($ourl, 0, -1);
    }
    if(substr($nurl, -1) == '/'){
      $nurl = substr($nurl, 0, -1);
    }  
    $ct = update_datatable($ourl, $nurl, 'options', 'option_value', 'option_id');
    $message = "$ct URLs found at wp_options.<br/>\n";
    $ct = update_datatable($ourl, $nurl, 'postmeta', 'meta_value', 'meta_id');
    $message .= "$ct URLs found at wp_postmeta.<br/>\n";
    $ct = update_datatable($ourl, $nurl, 'posts', 'post_content', 'ID');
    $message .= "$ct URLs found at wp_posts.<br/>\n";
    update_datatable($ourl, $nurl, 'posts', 'guid', 'ID');
    $message .= "All URLs are updated.";
  }
}

function update_datatable($search, $replace, $tablename, $field, $key) {
  global $wpdb;
  $select = "SELECT * FROM ".$wpdb->$tablename." WHERE $field like '%".$search."%'";
  $rows = $wpdb->get_results( $select );
  foreach($rows as $row) {
    if($usobj = unserialize($row->$field)){
      total_str_replace($search, $replace, $usobj);
      $new_value = serialize($usobj);
    } else {
      $new_value = $row->$field;
      total_str_replace($search, $replace, $new_value);
    }
    $wpdb->update($wpdb->$tablename, array($field => $wpdb->escape($new_value)), array($key => $row->$key), '%s', '%d');
  }
  return count($rows);
}

function total_str_replace($search, $replace, &$thing) {
  if(is_string($thing)){
    $thing = str_replace($search , $replace, $thing);
  } elseif(is_array($thing) || is_object($thing)) {
    foreach($thing as $key => $val){
      if(is_array($thing)){
        total_str_replace($search , $replace, $thing[$key]);
      } else {
        total_str_replace($search , $replace, $thing->$key);
      }
    }
  }
}

function getSiteurl()
{
  global $wpdb;
  $sql = "SELECT option_value FROM wp_options WHERE option_name='siteurl'";
  $results = $wpdb->get_results( $sql );
  return $results[0]->option_value;
}

if(empty($ourl)) {
  $ourl = getSiteurl();
  $ourl = str_replace('http://', '', str_replace('https://', '', $ourl));
}
?>

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
  <title>Update domain name</title>
  <style type="text/css">
  body, input, td { font-family:Calibri, Arial, Verdana; font-size:12px;}
  table {border: 1px solid #ccc; border-width:1px 1px 0 0; width: 830px;}
  td {border: 1px solid #ccc; border-width:0 0 1px 1px; padding:4px;}
  .messages {border: 1px solid #ccc; background:#fafafa; color:green; padding:6px; width: 818px; margin-bottom:6px;}
  .messages .error {color:red;}
  </style>
</head>

<body>
  <?php if($message):?>
  <div class="messages">
    <?php echo $message;?>
  </div>
  <?php endif;?>
  <form action="domain_update.php" method="post">
    <table cellspacing="0" cellpadding="0">
      <tr>
        <td>&nbsp;</td>
        <td>
          Please do not start with http:// and end with /
        </td>
      </tr>
      <tr>
        <td width="160" align="right">
          <label for="ourl">Original URL: </label>
        </td>
        <td>
          <input type="text" size="100" name="ourl" value="<?php echo $ourl;?>"/> 
        </td>
      </tr>
      <tr>
        <td width="160" align="right">
          <label for="nurl">New URL: </label>
        </td>
        <td>
          <input type="text" size="100" name="nurl" value="<?php echo $nurl;?>"/> 
        </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>
          <input type="submit" value="Submit"/>
        </td>
      </tr>
    </table>     
  </form>
</body>
</html>
